package com.zenith.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.storage.mapper.StorageProviderMapper;
import com.zenith.storage.po.domain.StorageProvider;
import com.zenith.storage.po.vo.StorageProviderVO;
import com.zenith.storage.service.StorageProviderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 存储提供商服务实现类
 * @author 22071
 */
@Slf4j
@Service
@RequiredArgsConstructor // 自动注入构造函数
public class StorageProviderServiceImpl extends ServiceImpl<StorageProviderMapper, StorageProvider> implements StorageProviderService {
    
    private final StorageProviderMapper storageProviderMapper;
    
    @Override
    public List<StorageProviderVO> listProviders() {
        List<StorageProvider> storageProviders = storageProviderMapper.selectList(new LambdaQueryWrapper<StorageProvider>()
                .eq(StorageProvider::getStatus, 1));
        return storageProviders.stream().map(storageProvider -> {
            StorageProviderVO storageProviderVO = new StorageProviderVO();
            BeanUtils.copyProperties(storageProvider, storageProviderVO);
            return storageProviderVO;
        }).toList();
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addProvider(StorageProvider provider) {
        // 如果设置为默认，则需要将其他提供商设置为非默认
        if (provider.getIsDefault() != null && provider.getIsDefault() == 1) {
            storageProviderMapper.update(null, new LambdaUpdateWrapper<StorageProvider>()
                    .set(StorageProvider::getIsDefault, 0));
        }
        return storageProviderMapper.insert(provider) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateProvider(StorageProvider provider) {
        // 如果设置为默认，则需要将其他提供商设置为非默认
        if (provider.getIsDefault() != null && provider.getIsDefault() == 1) {
            storageProviderMapper.update(null, new LambdaUpdateWrapper<StorageProvider>()
                    .set(StorageProvider::getIsDefault, 0));
        }
        return storageProviderMapper.updateById(provider) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteProvider(Long id) {
        return storageProviderMapper.deleteById(id) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean setDefaultProvider(Long id) {
        // 将所有提供商设置为非默认
        storageProviderMapper.update(null, new LambdaUpdateWrapper<StorageProvider>()
                .set(StorageProvider::getIsDefault, 0));
        
        // 将指定提供商设置为默认
        return storageProviderMapper.update(null, new LambdaUpdateWrapper<StorageProvider>()
                .eq(StorageProvider::getId, id)
                .set(StorageProvider::getIsDefault, 1)) > 0;
    }
    
    @Override
    public StorageProviderVO getDefaultProvider() {
        StorageProvider storageProvider = storageProviderMapper.selectOne(new LambdaQueryWrapper<StorageProvider>()
                .eq(StorageProvider::getIsDefault, 1)
                .eq(StorageProvider::getStatus, 1));

        StorageProviderVO storageProviderVO = new StorageProviderVO();
        BeanUtils.copyProperties(storageProvider, StorageProviderVO.class);
        return storageProviderVO;
    }
}